package com.lingtu.shopwxserver.utils;

import java.util.Arrays;

/**
 * @ClassName SignUtil
 * @Description 请求校验工具类
 * @Author MaShuai
 * @Date 2018/11/21 9:36
 * @Version 1.0
 **/
public class SignUtil {

    /**
     *  与微信公众号接口配置信息中的 Token 要一致
     */
    private static String token = "shop";

    /**
     * @Description 验证签名
     * @Param [signature, timestamp, nonce]
     * @return boolean
     */
    public static boolean checkSignature(String signature, String timestamp, String nonce) {
        String[] arr = new String[]{token, timestamp, nonce};
        // 將 token timestamp nonce 三个参数进行字典序排序
        Arrays.sort(arr);
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            buffer.append(arr[i]);
        }
        // 进行sha1加密
        String temp = SHA1.encode(buffer.toString());
        // 与微信提供的signature进行匹对
        return signature.equals(temp);
    }
}
